package com.huasoft.servlet.user;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.huasoft.dao.UserDao;
import com.huasoft.domain.Right;
import com.huasoft.domain.User;

public class NeedModifiedUser {
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (!testRight(request, response)) {
			request.setAttribute("noright", "没有权限");
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		} else {
			Integer id = Integer.valueOf(request.getParameter("id"));
			UserDao userDao = new UserDao();
			User needMuser = userDao.findById(id);
			request.setAttribute("needMuser", needMuser);
			request.getRequestDispatcher("/users/users_modify.jsp").forward(
					request, response);
		}
	}
/**
 * �ж��û��Ƿ���Ȩ��ִ�� �޸��û� �Ĳ���
 * @param request
 * @param response
 * @return
 * @throws UnsupportedEncodingException
 */
	public boolean testRight(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		boolean ownright = false;
		HttpSession session = request.getSession();
		List<Right> rights = (List<Right>) session.getAttribute("rights");
		String rightstr = (String) request.getParameter("right");
		rightstr = new String(rightstr.getBytes("iso8859-1"), "gbk");
		System.out.println(rightstr);
		for (Right right : rights) {
			if (rightstr.equals(right.getName())) {
				ownright = true;
				break;
			}
		}
		return ownright;
	}
}